package com.useeinfo.oa.open.admin;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.logger.biz.FacilityDataLogBiz;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * @auther: bin.liu
 * @Date: 2019/1/4 13:37
 * @Description:
 */
@Controller
@RequestMapping("/web/admin/facilityDataLog")
public class FacilityDataLogManageAction {

    @Autowired
    private FacilityDataLogBiz facilityDataLogBiz;

    @RequestMapping("/list")
    public ModelAndView list() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("pages/admin/facilityData/facilityDataLogList");
        return modelAndView;
    }

    @RequestMapping("/listJson")
    @ResponseBody
    public JSONObject listJson(@RequestParam(value = "pageNo", required = false) String pageNowParam,
                               @RequestParam(value = "pageSize", required = false) String pageSizeParam,
                               @RequestParam(value = "facilityDataName", required = false) String facilityDataName,
                               @RequestParam(value = "content", required = false) String content,
                               @RequestParam(value = "startTime", required = false) String startTimeParam,
                               @RequestParam(value = "endTime", required = false) String endTimeParam,
                               @RequestParam(value = "orderBy", required = false) String orderBy,
                               HttpServletRequest request) {
        QueryParam queryParam = new QueryParam(pageNowParam, pageSizeParam);
        queryParam.getSqlMap().put("facilityDataName", facilityDataName);
        queryParam.getSqlMap().put("content", content);
        queryParam.getSqlMap().put("startTime",startTimeParam);
        queryParam.getSqlMap().put("endTime",endTimeParam);
        if (StringUtils.isNotBlank(orderBy)) {
            queryParam.setSqlOrder("order by " + orderBy);
        }
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        if (enterprise != null) {
            queryParam.getSqlMap().put("enterpriseId", enterprise.getId() + "");
        }
        return facilityDataLogBiz.findJSONList(queryParam);
    }
}
